package com.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.community.pojo.Consumer;
import com.community.mapper.ConsumerMapper;
import com.community.service.IConsumerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer> implements IConsumerService {
    @Autowired
    private ConsumerMapper consumerMapper;

    /**
     * 前台用户注册
     *
     * @param consumers
     */
    @Override
    public void addConsumers(Consumer consumers) {
        if (consumers != null)
            this.save(consumers);
    }

    @Override
    public PageUtils<Consumer> getConsumersList(Consumer consumers, int pageNo, int pageSize) {
        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        if (consumers != null) {
            if (!StringUtils.isEmpty(consumers.getNickName())) {
                wrapper.like("nick_name", consumers.getNickName());
            }
            if (!StringUtils.isEmpty(consumers.getPhone())) {
                wrapper.eq("phone", consumers.getPhone());
            }
            if (!StringUtils.isEmpty(consumers.getRealName())) {
                wrapper.like("real_name", consumers.getRealName());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        PageHelper.orderBy("consumer_id");
        List<Consumer> list = consumerMapper.queryComsAll(consumers);

        PageInfo<Consumer> pageInfo = new PageInfo<>(list);

        PageUtils<Consumer> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public void delConsumer(Consumer consumers) {
        if (consumers != null && consumers.getConsumerId() != null) {
            // 构建更新的值和条件
            UpdateWrapper<Consumer> updateWrapper = new UpdateWrapper<>();

            // 设置要更新的列和新值
            updateWrapper.set("status", 0)
                    .eq("consumer_id", consumers.getConsumerId())
                    .eq("status", "C001-1");
            // 确保仅更新未删除的记录

            // 执行更新操作
            this.update(consumers, updateWrapper);
        }
    }

    @Override
    public void delsConsumer(List<Consumer> consumersList) {
        if (consumersList != null && !consumersList.isEmpty()) {
            // 构建更新的值和条件
            UpdateWrapper<Consumer> updateWrapper = new UpdateWrapper<>();

            // 设置要更新的列和新值
            updateWrapper.set("status", 1)
                    .in("consumer_id", consumersList.stream().map(Consumer::getConsumerId).toArray());

            // 执行更新操作
            this.update(updateWrapper);
        }
    }

    /**
     * 冻结/启用账户
     *
     * @param consumer
     */
    @Override
    public void switchState(Consumer consumer) {
        if (consumer != null) {
            this.updateById(consumer);
        }
    }
}


